import $http from '../../http/http.js'
import apiSetting from '../../http/apiSetting.js'
let dayjs = require('dayjs');
let app = getApp();

Page({
  data: {
    isJoinDataFactory: false
  },

  // 页面加载
  onLoad(query) {
    this.login()
  },
  onReady() {
    // 页面加载完成
  },
  onShow() {
    // 页面显示
  },

  // 登录
  login() {
    let that = this
    dd.showLoading({
      content: '载入中...',
    });
    dd.getAuthCode({
      success: function (res) {
        $http(apiSetting.login, {
          authCode: res.authCode,
          corpId: app.globalData.corpId
        }).then((data) => {
          app.globalData.token = data.result.token
          app.globalData.userId = data.result.userId
          app.globalData.unionId = data.result.unionId
          app.globalData.devCorpId = data.result.devCorpId
          console.log(data)
          console.log("isAdmin:" + data.result.isAdmin)
          console.log("isJoinDataFactory:" + data.result.isJoinDataFactory)
          console.log("isVerifyPhone:" + data.result.isVerifyPhone)
          // -------------测试分界---------------------
          // that.data.isJoinDataFactory = true
          // dd.hideLoading();
          // dd.reLaunch({
          //   url: '/pages/login/login'
          // })
          // return
          // ----------------END-----------------------
          // 是否入驻数字工厂 0：未入驻，1：已入驻
          if (data.result.isJoinDataFactory == 0) {
            that.data.isJoinDataFactory = false
            // 是否是管理员
            if (data.result.isAdmin) {
              dd.hideLoading();
              dd.confirm({
                title: '温馨提示',
                content: '未入驻数字工厂，是否前往',
                confirmButtonText: '立即前往',
                cancelButtonText: '暂不需要',
                success: (result) => {
                  if (`${result.confirm}` == 'true') {
                    dd.navigateTo({
                      url: '/pages/webView/webView?type=H5'
                    })
                  }
                }
              });
            } else {
              dd.hideLoading();
              dd.alert({ content: "请联系组织管理员开通数字工厂" });
            }
          } else {
            // 是否绑定手机 0：未绑定，1：已绑定
            if (data.result.isVerifyPhone == 0) {
              dd.hideLoading();
              dd.reLaunch({
                url: '/pages/login/login'
              })
            } else {
              dd.hideLoading();
              that.data.isJoinDataFactory = true
              that.factoryTreeCheck()
            }
          }
        })
      },
      fail: function (err) {
        dd.hideLoading();
        dd.alert({ content: '获取authCode失败' });
        console.log(err)
      },
      complete: function (res) {
        console.log(res)
      }
    });
    // dd.getAuthCode({
    //   success: function (res) {
    //     console.log('authCode:' + res.authCode)
    //   },
    //   fail: function (err) {
    //     console.log(err)
    //   }
    // });
  },

  // 查询存储树是否存在
  factoryTreeCheck() {
    let that = this
    dd.getStorage({
      key: 'selectFacScope',
      success: function (res) {
        if (res.data) {
          let key = res.data[0].key
          $http(apiSetting.factoryTreeCheck, {
            id: key
          }).then((data) => {
            if (data.code == 0) {
              if (!data.result) {
                dd.removeStorage({
                  key: 'selectFacScope',
                  success: function () {
                    console.log('删除成功')
                  }
                });
              }
            }
          })
        }
      },
      fail: function (res) {

      }
    });
  },

  handleTap(e) {
    let that = this
    if (!this.data.isJoinDataFactory) {
      dd.alert({
        content: '暂未入驻数字工厂',
        success: () => {
          that.login()
        },
      });
      return
    }
    let val = e.target.dataset.val
    let url = '/pages/' + val + '/' + val
    dd.navigateTo({
      url: url
    })
  },

  getFactoryTree() {
    let that = this
    $http(apiSetting.factoryTree, {}).then((data) => {
      console.log(data)
    })
  }
});
